using System;
using System.Data;
using System.IO;
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace webapp4U
{
   public static class Globals
   {
      public readonly static TheBeerHouseSection Settings = (TheBeerHouseSection)WebConfigurationManager.GetSection("theBeerHouse");

      public static string CurrentUserName
       {
           get
           {
               string userName = "";
               if (HttpContext.Current.User.Identity.IsAuthenticated)
                   userName = HttpContext.Current.User.Identity.Name;
               return userName;
           }
      }

       public static string CurrentUserID
       {
           get
           {
               string userID = "";
               if (HttpContext.Current.User.Identity.IsAuthenticated)
                   userID = webapp4U.BOL.User.GetUserIDByUserName(CurrentUserName);
               return userID;
           }
       }

       public static string CurrentUrl
       {
           get
           {
               string strUrl = HttpContext.Current.Request.Url.ToString().Trim();
               return strUrl;
           }
       }

       
      public static bool LangVietNam()
      {
           if (HttpContext.Current.Session["lang"] == "vi-vn" || HttpContext.Current.Session["lang"] == "" || HttpContext.Current.Session["lang"] == null)
               return true;
           else
               return false;
      }

      public static string GetLang(string strVietNam, string strEnglish)
      {
           if (LangVietNam() == true)
               return strVietNam;
           else
               return strEnglish;
      }

      //thu muc chua cac Album hinh
      public static String GetGalleryPath()
      {
          try
          {
              return PhysicalApplicationPath() + "//Gallery//";
          }
          catch { return ""; }
      }


      public static string ImagesUrl()
      {
          try
          {
              return ApplicationPath + "Images/";
          }
          catch { return ""; }
      }

      public static string BannerUrl()
      {
          try
          {
              return ApplicationPath + "Images/baner_VP/";
          }
          catch { return ""; }
      }
      public static string BannerTopUrl()
      {
          try
          {
              return ApplicationPath + "Images/baner_top_VP/";
          }
          catch { return ""; }
      }

      public static string ImagesUrlAdmin()
       {
           try
           {
               return ApplicationPath + "Admin/Images/";
           }
           catch { return ""; }
       }


       public static string ImagesUrlWebMaster()
       {
           try
           {
               return ApplicationPath + "WebMaster/Images/";
           }
           catch { return ""; }
       }

       
           
      public static string FullApplicationPath
      {
          get
          {
              return HttpContext.Current.Request.Url.AbsoluteUri.Replace(
                 HttpContext.Current.Request.Url.PathAndQuery, "") + ApplicationPath;
          }
      }

     
      public static string SuportOnline(int ichat)
      {
         
          string filePath = PhysicalApplicationPath() + "UserUpload/SuportOnline.txt";
          String chat1 = "";
          String chat2 = "";
          using (StreamReader sr = File.OpenText(filePath))
          {
              
              String input;
              int i=0;
              while ((input = sr.ReadLine()) != null)
              {
                  i++;
                  if (i == 1)
                      chat1 = input;
                  else if (i == 2)
                      chat2 = input;
                  else if (i > 2)
                      break;
              }
              
              sr.Close();
          }

          if (ichat == 1)
              return chat1;
          else
              return chat2;          
      }


       public static string ApplicationPath
       {
           get
           {
               string url = HttpContext.Current.Request.ApplicationPath;
               if (url.EndsWith("/"))
                   return url;
               else
                   return url + "/";
           }
       }


       public static string PhysicalApplicationPath()
       {
           return HttpContext.Current.Request.PhysicalApplicationPath.ToString();
       }


       public static int GetIntFromQueryString(string key)
       {
           int returnValue = -1;
           string queryStringValue;

           // Attempt to get the value from the query string
           //
           queryStringValue = HttpContext.Current.Request.QueryString[key];

           // If we didn't find anything, just return
           //
           if (queryStringValue == null)
               return returnValue;

           // Found a value, attempt to conver to integer
           //
           try
           {

               // Special case if we find a # in the value
               //
               if (queryStringValue.IndexOf("#") > 0)
                   queryStringValue = queryStringValue.Substring(0, queryStringValue.IndexOf("#"));

               returnValue = Convert.ToInt32(queryStringValue);
           }
           catch { }

           return returnValue;

       }


       public static string GetStringFromQueryString(string key)
       {
           string returnValue = "";
           string queryStringValue;

           // Attempt to get the value from the query string
           //
           queryStringValue = HttpContext.Current.Request.QueryString[key];

           // If we didn't find anything, just return
           //
           if (queryStringValue == null)
               return returnValue;

           // Found a value, attempt to conver to integer
           //
           try
           {

               // Special case if we find a # in the value
               //
               if (queryStringValue.IndexOf("#") > 0)
                   queryStringValue = queryStringValue.Substring(0, queryStringValue.IndexOf("#"));

               returnValue = Convert.ToString(queryStringValue);
           }
           catch { }

           return returnValue;
       }


       public static Int32 ConvertToInt32(object obj)
       {
           try
           {
               return Convert.ToInt32(obj);
           }
           catch { return -1; }
       }


       public static bool IsDate(string Text)
       {
           bool result = true;
           DateTime date;

           try
           {
               date = Convert.ToDateTime(Text);
           }
           catch
           {
               result = false;
           }

           return result;
       }

       public static bool IsHomePage()
       {
           string strUrl = Globals.CurrentUrl;
           if (strUrl.ToLower().IndexOf("default.aspx") > 0 && strUrl.ToLower().IndexOf("articles") < 0)
               return true;
           else
               return false;
       }

      

   }
}